package com.da.lon.wang.xlg.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.view.ProductsPopupWindow.CallBack3;

public class ProductsShowMessagesPopupWindow extends PopupWindow implements
        OnClickListener {

    private View mMenuView;
    private ImageView closeView;
    private TextView online_phone_tv;
    private TextView online_custome_tv;
    private Context mContext;
    private TextView pord_num_tv;
    private String phone;
    private CallBack3 myCallBack;

    public ProductsShowMessagesPopupWindow(Context context, String phone,
                                           CallBack3 myCallBack) {
        super(context);
        this.mContext = context;
        this.phone = phone;
        this.myCallBack = myCallBack;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mMenuView = inflater.inflate(
                R.layout.ouka_products_showmessages_popupwindow, null);

        // 设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.FILL_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int mMenuView_height = mMenuView.findViewById(R.id.pop_layout).getHeight();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height || y > (height + mMenuView_height)) {
                        dismiss();
                    }
                }
                return true;
            }
        });

        pord_num_tv = (TextView) mMenuView.findViewById(R.id.pord_num_tv);
        online_phone_tv = (TextView) mMenuView
                .findViewById(R.id.online_phone_tv);
        online_phone_tv.setText(phone);
        online_custome_tv = (TextView) mMenuView
                .findViewById(R.id.online_custome_tv);
        closeView = (ImageView) mMenuView.findViewById(R.id.detail_pop_close);
        closeView.setOnClickListener(this);
        online_phone_tv.setOnClickListener(this);
        online_custome_tv.setOnClickListener(this);

        // 初始化商品属性的控件
        initView(context);
    }

    private void initView(Context context) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.detail_pop_close:
                dismiss();
                break;
            // 在线客户
            case R.id.online_custome_tv:
                myCallBack.click(online_custome_tv, null, this);
                dismiss();
                break;
            // 拨打电话
            case R.id.online_phone_tv:
                if (!"".equals(phone)) {
                    Uri uri = Uri.parse("tel:" + phone);
                    Intent intent = new Intent(Intent.ACTION_DIAL, uri);
                    mContext.startActivity(intent);
                    dismiss();
                }
                break;

            default:
                break;
        }
    }

}
